﻿                
/********************************************************************************
*********************************************************************************

                        AreaDetecVer2 Area Detection version2
                           copyright by Vincent Liu @2016*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using HalconDotNet;

namespace AreaDetecVer2
{
    static class AreaDetect
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
public partial class Halcon_AreaDetect

{
    //类成员定义
    private HTuple WinHandle;//窗口句柄
    private HObject hobj_OrgImg; //original image
    private HTuple htup_ImgWidth;//image width
    private HTuple htup_ImgHeight;//image height
    private HTuple htup_Meanbackgroud;//background average gray scale
    private HTuple AmplitudeThreshold;//灰度阈值
    private HTuple htup_RowtestLU;//test area pt1.y
    private HTuple htup_ColumntestLU;//test area pt1.x
    private HTuple htup_RowtestRD;//test area pt2.y
    private HTuple htup_ColumntestRD;//test area pt2.x
    private HTuple htup_TestMeanGrey;//test area average gray scale
    private HTuple htup_topside_LU_Row;//top detect rectangle top_left pt.y
    private HTuple htup_topside_LU_Column;//top detect rectangle top_left pt.x
    private HTuple htup_topside_RD_Row, htup_topside_RD_Column;
    private HTuple htup_TopsideRowArray, htup_TopsideColumnArray;//point array (topside)
    private HTuple htup_leftside_LU_Row, htup_leftside_LU_Column, htup_leftside_RD_Row, htup_leftside_RD_Column;//左检测框的位置
    private HTuple htup_LeftsideRowArray, htup_LeftsideColumnArray;//左检测框检测到的点集
    private HTuple htup_bottom_LU_Row, htup_bottom_LU_Column, htup_bottom_RD_Row, htup_bottom_RD_Column;//下检测框的位置
    private HTuple htup_BottomRowArray, htup_BottomColumnArray;//下检测框检测到的点集
    private HTuple htup_rightside_LU_Row, htup_rightside_LU_Column, htup_right_RD_Row, htup_rightside_RD_Column;//右检测框的位置
    private HTuple htup_RightsideRowArray, htup_RightColumnArray;//右检测框检测到的点集
    private HTuple htup_TopLeftRow, htup_TopLeftCol,
                   htup_TopRightRow, htup_ToprightCol,
                   htup_BottomLeftRow, htup_BottomLeftCol,
                   htup_BottomRightRow, htup_BottomRightCol;//4个角点
    HTuple htup_centerRow, htup_centerColumn ;
    private HTuple htup_AngleTopX, htup_AngleBottomX, htup_AngleLeftY, htup_AngleRightY;//边与X-Y轴夹角，逆时针方向为正方向
    private HTuple htup_AngleTopLeft, htup_AngleTopRight,
                   htup_AngleBottomLeft, htup_AngleBottomRight;//四个边角
    private HTuple htup_Rowlu, htup_Columnlu, htup_Radiuslu,//左上角弧拟合圆的圆点与半径
                   htup_Rowru, htup_Columnru, htup_Radiusru,//右上角弧拟合圆的圆点与半径
                   htup_Rowld, htup_Columnld, htup_Radiusld,//左下角弧拟合圆的圆点与半径
                   htup_Rowrd, htup_Columnrd, htup_Radiusrd,//右下角弧拟合圆的圆点与半径
                   htup_Rowlurd, htup_Columnlurd, htup_Radiuslurd,//左上右下角弧拟合圆的圆点与半径
                   htup_Rowldru, htup_Columnldru, htup_Radiusldru,//左下右上角弧拟合圆的圆点与半径
                   htup_Rowall, htup_Columnall, htup_Radiusall;//所有弧拟合圆的圆点与半径
    private HTuple htup_pt_row_lineleft_up, htup_pt_col_lineleft_up,//Left边与弧的上边交点
                   htup_pt_row_lineup_left, htup_pt_col_lineup_left,//Top边与弧的左边交点
                   htup_pt_row_lineup_right, htup_pt_col_lineup_right,//Top边与弧的左边交点
                   htup_pt_row_lineright_up, htup_pt_col_lineright_up, //Right边与弧的上交点
                   htup_pt_row_lineright_down, htup_pt_col_lineright_down,//Right边与弧的下交点
                   htup_pt_row_linedown_right, htup_pt_col_linedown_right,//Bottom边与弧的右交点
                   htup_pt_row_linedown_left, htup_pt_col_linedown_left,//Bottom边与弧的左交点
                   htup_pt_row_lineleft_down, htup_pt_col_lineleft_down;//Left边与弧的下边交点
    private HObject hobj_WholeROI;//电池片整个ROI
    private HTuple htup_Distance_upline , htup_Distance_rightline , htup_Distance_leftline, htup_Distance_downline;//边长
    private HTuple htup_Distance_updown, htup_Distance_leftright;
    private HTuple htup_LengthDiagonal1, htup_LengthDiagonal2;//对角线长度
    private HTuple htup_Angle_diagonalDeg;//对角线夹角
    private HTuple htup_hole_count;//孔洞个数
    private HTuple htup_CenterDiff;//同心度
    private HTuple htup_arc_leftup, htup_arc_rightdown, htup_arc_leftdown, htup_arc_rightup;//四个弧边长度
    private static double lengthEachpixel = 0.03758;//像元大小，需要相机标定后确认
    //四个弧边上的点集（用于拟合弧线或者直线）
    HTuple htup_TempRowEdgelu = new HTuple();
    HTuple htup_TempColumnEdgelu = new HTuple();
    HTuple htup_TempRowEdgeru = new HTuple();
    HTuple htup_TempColumnEdgeru = new HTuple();
    HTuple htup_TempRowEdgeld = new HTuple();
    HTuple htup_TempColumnEdgeld = new HTuple();
    HTuple htup_TempRowEdgerd = new HTuple();
    HTuple htup_TempColumnEdgerd = new HTuple();
    HTuple htup_Parameter = new HTuple();//保留的参数，如检测框位置等
    //HTuple htup_setname;//保存与读取的名称
    // HTuple htup_IsPolycrystal;//单晶多晶判断标志
    double dbl_StandEdgeDistance, dbl_StandEdgeLength, dbl_StandArcAngle,dbl_StandDiagonalLength,
        dbl_StandDiagonalAngle, dbl_StandArcLength,dbl_StandArcRadius, dbl_MaxCenterDiff;
    //标准边距、标准边长、标准边角角度、标准对角线长度和角度、标准弧长、硅棒半径、最大可接受偏心距
    double dbl_LengthPrecision, dbl_AnglePrecision;//精度需求
    int int_ObjSerialNum;//硅片编号
    bool bl_IsAreaDetectOK;//最终检测NG判定


    // Main procedure 
    //读入文件，目前无法从相机内存中读取只写成读文件的操作
    public void LoadImg(HTuple H_winHandle ,string Imgpath) {//Load Image
        this.htup_ImgWidth = null; this.htup_ImgHeight = null ;
        HOperatorSet.ReadImage(out hobj_OrgImg, Imgpath);
        HOperatorSet.DispObj(hobj_OrgImg, H_winHandle);
        HOperatorSet.GetImageSize(hobj_OrgImg, out htup_ImgWidth, out htup_ImgHeight);
        HOperatorSet.SetPart(H_winHandle, 0, 0, htup_ImgHeight,htup_ImgWidth);
        HOperatorSet.DispObj(hobj_OrgImg, H_winHandle);
    }
    //面阵检测初始化
    public void DetecInit(HTuple winhandle) {
        //面阵程序初始化，可以读入配置，读入上次检测框位置等
        WinHandle = winhandle;
      
    }

    //**********************************检测框定位**********************************
    public void LocateDetecRec() {
        //******背景灰度选取********
        HObject roi_backgroud;
        HTuple Rowbackground1 = null;
        HTuple Columnbackground1 = null;
        HTuple Rowbackground2 = null;
        HTuple Columnbackground2 = null;
        this.htup_Meanbackgroud=null;
        HTuple Deviationbackgroud=null;
        HOperatorSet.SetColor(WinHandle, "red");
        MessageBox.Show("选取背景");
        HOperatorSet.DrawRectangle1(WinHandle, out Rowbackground1, out Columnbackground1,
        out Rowbackground2, out Columnbackground2);
        HOperatorSet.GenEmptyObj(out roi_backgroud);
        HOperatorSet.GenRectangle1(out roi_backgroud, Rowbackground1, Columnbackground1,
        Rowbackground2, Columnbackground2);
        HOperatorSet.Intensity(roi_backgroud, hobj_OrgImg, out htup_Meanbackgroud,
        out Deviationbackgroud);
        roi_backgroud.Dispose();

        //******测试区灰度选取(电池片)********  
        this. htup_RowtestLU = null;
        this. htup_ColumntestLU = null;
        this. htup_RowtestRD = null;
        this. htup_ColumntestRD = null;   
        MessageBox.Show("选取测试区");
        HOperatorSet.DrawRectangle1(WinHandle, out htup_RowtestLU, out htup_ColumntestLU,
        out htup_RowtestRD, out htup_ColumntestRD);

        //********************************上边界检测框***********************************
        MessageBox.Show("上边界检测框");
        htup_topside_LU_Row = null;htup_topside_LU_Column = null;htup_topside_RD_Row = null;htup_topside_RD_Column = null;
        HOperatorSet.DrawRectangle1(WinHandle, out htup_topside_LU_Row, out htup_topside_LU_Column,
            out htup_topside_RD_Row, out htup_topside_RD_Column);
     
        //********************************左边界检测框***********************************
      
        htup_leftside_LU_Row = null;htup_leftside_LU_Column = null;htup_leftside_RD_Row = null;htup_leftside_RD_Column = null;
        MessageBox.Show("左边界检测框");
        HOperatorSet.DrawRectangle1(WinHandle, out htup_leftside_LU_Row, out htup_leftside_LU_Column,
            out htup_leftside_RD_Row, out htup_leftside_RD_Column);

        //********************************下边界检测框***********************************
        htup_bottom_LU_Row = null; htup_bottom_LU_Column = null;
        htup_bottom_RD_Row = null; htup_bottom_RD_Column = null;
        MessageBox.Show("下边缘检测框")  ;
        HOperatorSet.DrawRectangle1(WinHandle, out htup_bottom_LU_Row, out htup_bottom_LU_Column,
            out htup_bottom_RD_Row, out htup_bottom_RD_Column);
        //********************************右边界检测框***********************************
        htup_rightside_LU_Row = null; htup_rightside_LU_Column = null;
        htup_right_RD_Row = null; htup_rightside_RD_Column = null;
        MessageBox.Show("右边界检测框");
        HOperatorSet.DrawRectangle1(WinHandle, out htup_rightside_LU_Row, out htup_rightside_LU_Column,
        out htup_right_RD_Row, out htup_rightside_RD_Column);
        
        //end of LocateDetecRec
    }
    public void GetLineEdgePt()
    {
        HObject roi_test;
        AmplitudeThreshold=null;
        HTuple TmpCtrl_Row = null;
        HTuple TmpCtrl_Column = null;
        HTuple TmpCtrl_Phi = null;
        HTuple TmpCtrl_Len1 = null;
        HTuple TmpCtrl_Len2 = null;
        this.htup_TestMeanGrey = null;
        HTuple Deviationtest = null;
        HTuple htup_Row_Measure = null;
        HTuple htup_Column_Measure = null;
        HTuple htup_Amplitude_Measure = null;
        HTuple htup_Distance_Measure = null;
        HTuple LengthMeasure = null;
        HTuple MsrHandle_Measure_01_0 = null;
        HOperatorSet.GenEmptyObj(out roi_test);
        HOperatorSet.GenRectangle1(out roi_test, htup_RowtestLU, htup_ColumntestLU, htup_RowtestRD,
            htup_ColumntestRD);
        HOperatorSet.Intensity(roi_test, hobj_OrgImg, out htup_TestMeanGrey, out Deviationtest);
        //计算阈值
        AmplitudeThreshold = (((htup_Meanbackgroud - htup_TestMeanGrey) / 3)).TupleAbs();

        roi_test.Dispose();
        //************************************************上边缘边界点寻找
        HTuple Num = 0, n_lineup = 200, n_lineleft = 200, n_linedown = 200, n_lineright = 200;
        htup_TopsideRowArray = new HTuple();
        htup_TopsideColumnArray = new HTuple();
        for (int Index = 0; Index < n_lineup; Index++)
        {
            TmpCtrl_Row = (htup_topside_LU_Row + htup_topside_RD_Row) / 2;
            TmpCtrl_Column = htup_topside_LU_Column + (((htup_topside_RD_Column - htup_topside_LU_Column) * Index) / (n_lineup - 1));
            TmpCtrl_Phi = (new HTuple(90)).TupleRad();
            TmpCtrl_Len1 = htup_topside_RD_Row - htup_topside_LU_Row;
            TmpCtrl_Len2 = 1;
            HOperatorSet.GenMeasureRectangle2(TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi,
                TmpCtrl_Len1, TmpCtrl_Len2, this.htup_ImgWidth, this.htup_ImgHeight, "nearest_neighbor",
                out MsrHandle_Measure_01_0);
            HOperatorSet.MeasurePos(hobj_OrgImg, MsrHandle_Measure_01_0, 2.9, AmplitudeThreshold,
                "all", "all", out htup_Row_Measure, out htup_Column_Measure, out htup_Amplitude_Measure,
                out htup_Distance_Measure);
            HOperatorSet.TupleLength(htup_Row_Measure, out LengthMeasure);
            if (LengthMeasure > 0)
            {
                if (htup_TopsideRowArray == null)
                    htup_TopsideRowArray = new HTuple();
                htup_TopsideRowArray[Num] = htup_Row_Measure.TupleSelect(0);
                if (htup_TopsideColumnArray == null)
                    htup_TopsideColumnArray = new HTuple();
                htup_TopsideColumnArray[Num] = htup_Column_Measure.TupleSelect(0);
                Num = Num + 1;
            }
            HOperatorSet.CloseMeasure(MsrHandle_Measure_01_0);
        }
        //***************************************************左边缘边界点寻找
        Num = 0;
        this.htup_LeftsideRowArray = new HTuple();
        this.htup_LeftsideColumnArray = new HTuple();
        for (int Index = 0; Index < n_lineleft; Index++)
        {
            TmpCtrl_Row = htup_leftside_LU_Row + (((htup_leftside_RD_Row - htup_leftside_LU_Row) * Index) / (n_lineleft - 1));
            TmpCtrl_Column = (htup_leftside_LU_Column + htup_leftside_RD_Column) / 2;
            TmpCtrl_Phi = (new HTuple(0)).TupleRad();
            TmpCtrl_Len1 = htup_leftside_RD_Column - htup_leftside_LU_Column;
            TmpCtrl_Len2 = 1;
            HOperatorSet.GenMeasureRectangle2(TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi,
                TmpCtrl_Len1, TmpCtrl_Len2, this.htup_ImgWidth, this.htup_ImgHeight, "nearest_neighbor",
                out MsrHandle_Measure_01_0);
            HOperatorSet.MeasurePos(hobj_OrgImg, MsrHandle_Measure_01_0, 2.9, AmplitudeThreshold,
                "negative", "all", out htup_Row_Measure, out htup_Column_Measure, out htup_Amplitude_Measure,
                out htup_Distance_Measure);
            HOperatorSet.TupleLength(htup_Row_Measure, out LengthMeasure);
            if ((int)(new HTuple(LengthMeasure.TupleGreater(0))) != 0)
            {
                if (this.htup_LeftsideRowArray == null)
                    this.htup_LeftsideRowArray = new HTuple();
                this.htup_LeftsideRowArray[Num] = htup_Row_Measure.TupleSelect(0);
                if (this.htup_LeftsideColumnArray == null)
                    this.htup_LeftsideColumnArray = new HTuple();
                this.htup_LeftsideColumnArray[Num] = htup_Column_Measure.TupleSelect(0);
                Num = Num + 1;
            }
            HOperatorSet.CloseMeasure(MsrHandle_Measure_01_0);
        }

        //
        //********************************************右边缘边界点
        Num = 0;
        htup_RightsideRowArray = new HTuple();
        htup_RightColumnArray = new HTuple();
        for (int Index = 0; Index < n_lineright; Index++)
        {
            TmpCtrl_Row = htup_rightside_LU_Row + (((htup_right_RD_Row - htup_rightside_LU_Row) * Index) / (n_lineright - 1));
            TmpCtrl_Column = (htup_rightside_LU_Column + htup_rightside_RD_Column) / 2;
            TmpCtrl_Phi = (new HTuple(0)).TupleRad();
            TmpCtrl_Len1 = htup_rightside_RD_Column - htup_rightside_LU_Column;
            TmpCtrl_Len2 = 1;
            HOperatorSet.GenMeasureRectangle2(TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi,
                TmpCtrl_Len1, TmpCtrl_Len2, this.htup_ImgWidth, this.htup_ImgHeight, "nearest_neighbor",
                out MsrHandle_Measure_01_0);
            HOperatorSet.MeasurePos(hobj_OrgImg, MsrHandle_Measure_01_0, 2.9, AmplitudeThreshold,
                "positive", "all", out htup_Row_Measure, out htup_Column_Measure, out htup_Amplitude_Measure,
                out htup_Distance_Measure);
            HOperatorSet.TupleLength(htup_Row_Measure, out LengthMeasure);
            if ((int)(new HTuple(LengthMeasure.TupleGreater(0))) != 0)
            {
                if (htup_RightsideRowArray == null)
                    htup_RightsideRowArray = new HTuple();
                htup_RightsideRowArray[Num] = htup_Row_Measure.TupleSelect(0);
                if (htup_RightColumnArray == null)
                    htup_RightColumnArray = new HTuple();
                htup_RightColumnArray[Num] = htup_Column_Measure.TupleSelect(0);
                Num = Num + 1;
            }
            HOperatorSet.CloseMeasure(MsrHandle_Measure_01_0);
        }

        //
        //************************************************下边缘边界点寻找
        Num = 0;
        htup_BottomRowArray = new HTuple();
        htup_BottomColumnArray = new HTuple();
        for (int Index = 0; Index < n_linedown; Index++)
        {
            TmpCtrl_Row = (htup_bottom_RD_Row + htup_bottom_LU_Row) / 2;
            TmpCtrl_Column = htup_bottom_LU_Column + (((htup_bottom_RD_Column - htup_bottom_LU_Column) * Index) / (n_linedown - 1));
            TmpCtrl_Phi = (new HTuple(90)).TupleRad();
            TmpCtrl_Len1 = htup_bottom_RD_Row - htup_bottom_LU_Row;
            TmpCtrl_Len2 = 1;
            HOperatorSet.GenMeasureRectangle2(TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi,
                TmpCtrl_Len1, TmpCtrl_Len2, this.htup_ImgWidth, this.htup_ImgHeight, "nearest_neighbor",
                out MsrHandle_Measure_01_0);
            HOperatorSet.MeasurePos(hobj_OrgImg, MsrHandle_Measure_01_0, 2.9, AmplitudeThreshold,
                "all", "all", out htup_Row_Measure, out htup_Column_Measure, out htup_Amplitude_Measure,
                out htup_Distance_Measure);
            HOperatorSet.TupleLength(htup_Row_Measure, out LengthMeasure);
            if ((int)(new HTuple(LengthMeasure.TupleGreater(0))) != 0)
            {
                if (htup_BottomRowArray == null)
                    htup_BottomRowArray = new HTuple();
                htup_BottomRowArray[Num] = htup_Row_Measure.TupleSelect(0);
                if (htup_BottomColumnArray == null)
                    htup_BottomColumnArray = new HTuple();
                htup_BottomColumnArray[Num] = htup_Column_Measure.TupleSelect(0);
                Num = Num + 1;
            }
            HOperatorSet.CloseMeasure(MsrHandle_Measure_01_0);
        }
    }
    public void WritepPrameter()
    {
        HTuple htup_Parameter;
        htup_Parameter = new HTuple();
        htup_Parameter[0] = htup_Meanbackgroud;//保留背景灰度值
        //背景检测框位置
        htup_Parameter[1] = htup_RowtestLU;
        htup_Parameter[2] = htup_ColumntestLU;
        htup_Parameter[3] = htup_RowtestRD;
        htup_Parameter[4] = htup_ColumntestRD;
        //电池片四个边检测框位置
        htup_Parameter[5] = htup_topside_LU_Row;
        htup_Parameter[6] = htup_topside_LU_Column;
        htup_Parameter[7] = htup_topside_RD_Row;
        htup_Parameter[8] = htup_topside_RD_Column;
        htup_Parameter[9] = htup_leftside_LU_Row;
        htup_Parameter[10] = htup_leftside_LU_Column;
        htup_Parameter[11] = htup_leftside_RD_Row;
        htup_Parameter[12] = htup_leftside_RD_Column;
        htup_Parameter[13] = htup_bottom_LU_Row;
        htup_Parameter[14] = htup_bottom_LU_Column;
        htup_Parameter[15] = htup_bottom_RD_Row;
        htup_Parameter[16] = htup_bottom_RD_Column;
        htup_Parameter[17] = htup_rightside_LU_Row;
        htup_Parameter[18] = htup_rightside_LU_Column;
        htup_Parameter[19] = htup_right_RD_Row;
        htup_Parameter[20] = htup_rightside_RD_Column;
        HTuple  htup_setname = "set1";
        HOperatorSet.WriteTuple(htup_Parameter, htup_setname);


    }
    public void ReadParameter()
    {
        HTuple htup_Parameter;
        htup_Parameter = new HTuple();
       HTuple htup_setname = "set1";
        HOperatorSet.ReadTuple(htup_setname, out htup_Parameter);
        htup_Meanbackgroud = htup_Parameter[0];//保留背景灰度值
        //背景检测框位置
        htup_RowtestLU = htup_Parameter[1];
        htup_ColumntestLU = htup_Parameter[2];
        htup_RowtestRD = htup_Parameter[3];
        htup_ColumntestRD = htup_Parameter[4];
        //电池片四个边检测框位置
        htup_topside_LU_Row = htup_Parameter[5];
        htup_topside_LU_Column = htup_Parameter[6];
        htup_topside_RD_Row = htup_Parameter[7];
        htup_topside_RD_Column = htup_Parameter[8];
        htup_leftside_LU_Row = htup_Parameter[9];
        htup_leftside_LU_Column = htup_Parameter[10];
        htup_leftside_RD_Row = htup_Parameter[11];
        htup_leftside_RD_Column = htup_Parameter[12];
        htup_bottom_LU_Row = htup_Parameter[13];
        htup_bottom_LU_Column = htup_Parameter[14];
        htup_bottom_RD_Row = htup_Parameter[15];
        htup_bottom_RD_Column = htup_Parameter[16];
        htup_rightside_LU_Row = htup_Parameter[17];
        htup_rightside_LU_Column = htup_Parameter[18];
        htup_right_RD_Row = htup_Parameter[19];
        htup_rightside_RD_Column = htup_Parameter[20];



    }
    //*************************求4个角点坐标*********************************
    public void GetCornerPt() {
        //*****************根据各个边的点集拟合边*************************
        //*********************Topline*****************
        HObject hobj_ContourTopline;
        HTuple htup_toplineBeginRow = null;
        HTuple htup_toplineBeginCol = null;
        HTuple htup_toplineEndRow = null;
        HTuple htup_toplineEndnCol = null;
        HOperatorSet.GenEmptyObj(out hobj_ContourTopline);
        HTuple htup_Nr = null; HTuple htup_Nc = null; HTuple htup_Dist = null;
        HOperatorSet.GenContourPolygonXld(out hobj_ContourTopline, htup_TopsideRowArray, htup_TopsideColumnArray);
        HOperatorSet.FitLineContourXld(hobj_ContourTopline, "tukey", -1, 0, 5, 2, out htup_toplineBeginRow,
            out htup_toplineBeginCol, out htup_toplineEndRow, out htup_toplineEndnCol, out htup_Nr, out htup_Nc,
            out htup_Dist);

        //***********************leftLine******************
        HObject hobj_ContourLeftline;
        HTuple htup_LeftlineBeginRow = null;
        HTuple htup_LeftlineEndRow = null;
        HTuple htup_LeftlineBeginCol = null;
        HTuple htup_leftlineEndcol = null;
        HOperatorSet.GenEmptyObj(out hobj_ContourLeftline);
        HOperatorSet.GenContourPolygonXld(out hobj_ContourLeftline, htup_LeftsideRowArray,
            htup_LeftsideColumnArray);
        HOperatorSet.FitLineContourXld(hobj_ContourLeftline, "tukey", -1, 0, 5, 2, out htup_LeftlineBeginRow,
            out htup_LeftlineBeginCol, out htup_LeftlineEndRow , out htup_leftlineEndcol, out htup_Nr, out htup_Nc,
            out htup_Dist);

        //***********************rightLine*****************
        HTuple htup_RightlineBeginRow = null;
        HTuple htup_RightlineEndRow = null;
        HTuple htup_RightlineBeginCol = null;
        HTuple htup_RightlineEndcol = null;
        HObject hobj_ContourRightline;
        HOperatorSet.GenEmptyObj(out hobj_ContourRightline);
        HOperatorSet.GenContourPolygonXld(out hobj_ContourRightline, htup_RightsideRowArray, htup_RightColumnArray);
        HOperatorSet.FitLineContourXld(hobj_ContourRightline, "tukey", -1, 0, 5, 2, out htup_RightlineBeginRow,
            out htup_RightlineBeginCol, out htup_RightlineEndRow, out htup_RightlineEndcol, out htup_Nr, out htup_Nc,
            out htup_Dist);
       
        //***********************BottomLine*****************
        HObject hobj_ContourBottomline;
        HTuple htup_BottomlineBeginRow = null;
        HTuple htup_BottomlineEndRow = null;
        HTuple htup_BottomlineBeginCol = null;
        HTuple htup_BottomlineEndCol = null;
        HOperatorSet.GenEmptyObj(out hobj_ContourBottomline);
        HOperatorSet.GenContourPolygonXld(out hobj_ContourBottomline, htup_BottomRowArray, htup_BottomColumnArray);
        HOperatorSet.FitLineContourXld(hobj_ContourBottomline, "tukey", -1, 0, 5, 2, out htup_BottomlineBeginRow,
            out htup_BottomlineBeginCol, out htup_BottomlineEndRow , out htup_BottomlineEndCol, out htup_Nr, out htup_Nc,
            out htup_Dist);
       

        //***********************************求角点***************************
       

        HTuple htup_IsOverlapping = null;
        //左上角点
        HOperatorSet.IntersectionLines(htup_toplineBeginRow, htup_toplineBeginCol, htup_toplineEndRow, htup_toplineEndnCol,
            htup_LeftlineBeginRow, htup_LeftlineBeginCol, htup_LeftlineEndRow, htup_leftlineEndcol, out htup_TopLeftRow,
            out htup_TopLeftCol, out htup_IsOverlapping);
        //右上角点
        HOperatorSet.IntersectionLines(htup_toplineBeginRow, htup_toplineBeginCol, htup_toplineEndRow, htup_toplineEndnCol,
            htup_RightlineBeginRow, htup_RightlineBeginCol, htup_RightlineEndRow, htup_RightlineEndcol, out htup_TopRightRow,
            out htup_ToprightCol, out htup_IsOverlapping);
        //左下角点
        HOperatorSet.IntersectionLines(htup_BottomlineBeginRow, htup_BottomlineBeginCol, htup_BottomlineEndRow,
            htup_BottomlineEndCol, htup_LeftlineBeginRow, htup_LeftlineBeginCol, htup_LeftlineEndRow, htup_leftlineEndcol,
            out htup_BottomLeftRow, out htup_BottomLeftCol, out htup_IsOverlapping);
        //右下角点
        HOperatorSet.IntersectionLines(htup_BottomlineBeginRow, htup_BottomlineBeginCol, htup_BottomlineEndRow,
            htup_BottomlineEndCol, htup_RightlineBeginRow, htup_RightlineBeginCol, htup_RightlineEndRow, htup_RightlineEndcol,
            out htup_BottomRightRow, out htup_BottomRightCol, out htup_IsOverlapping);


         
    }
   
    //*************************计算边角角度**********************************
    public void GetConerAngle()
    {
        
        htup_AngleTopX = ((-((((htup_TopRightRow - htup_TopLeftRow) / (((((htup_TopRightRow - htup_TopLeftRow) * (htup_TopRightRow - htup_TopLeftRow)) + ((htup_ToprightCol - htup_TopLeftCol) * (htup_ToprightCol - htup_TopLeftCol)))).TupleSqrt()
      ))).TupleAsin()))).TupleDeg();
        htup_AngleBottomX = ((-((((htup_BottomRightRow - htup_BottomLeftRow) / (((((htup_BottomRightRow - htup_BottomLeftRow) * (htup_BottomRightRow - htup_BottomLeftRow)) + ((htup_BottomRightCol - htup_BottomLeftCol) * (htup_BottomRightCol - htup_BottomLeftCol)))).TupleSqrt()
            ))).TupleAsin()))).TupleDeg();
        htup_AngleLeftY = (((((htup_BottomLeftCol - htup_TopLeftCol) / (((((htup_BottomLeftRow - htup_TopLeftRow) * (htup_BottomLeftRow - htup_TopLeftRow)) + ((htup_BottomLeftCol - htup_TopLeftCol) * (htup_BottomLeftCol - htup_TopLeftCol)))).TupleSqrt()
            ))).TupleAsin())).TupleDeg();
        htup_AngleRightY = (((((htup_BottomRightCol - htup_ToprightCol) / (((((htup_BottomRightRow - htup_TopRightRow) * (htup_BottomRightRow - htup_TopRightRow)) + ((htup_BottomRightCol - htup_ToprightCol) * (htup_BottomRightCol - htup_ToprightCol)))).TupleSqrt()
            ))).TupleAsin())).TupleDeg();


        //**********求出每个角的角度
        htup_AngleTopLeft = null; htup_AngleTopRight=null; htup_AngleBottomLeft = null; htup_AngleBottomRight=null;
        htup_AngleTopLeft = (90 + htup_AngleTopX) - htup_AngleLeftY;
        htup_AngleTopRight = (90 + htup_AngleRightY) - htup_AngleTopX;
        htup_AngleBottomLeft = (90 + htup_AngleLeftY) - htup_AngleBottomX;
        htup_AngleBottomRight = (90 + htup_AngleBottomX) - htup_AngleRightY;
    }
    //********************根据4个角点得到对角线*******************************
    public void GetDiagonal() {
        //********************对角线**********************
      
        HTuple htup_IsOverlapping = null;
        HOperatorSet.IntersectionLines(htup_TopLeftRow, htup_TopLeftCol, htup_BottomRightRow,
       htup_BottomRightCol, htup_BottomLeftRow, htup_BottomLeftCol, htup_TopRightRow, htup_ToprightCol,
       out htup_centerRow, out htup_centerColumn, out htup_IsOverlapping);

        //display
       
    }
    public void GetArcEdgePt()
    {





        //*************************************左上弧的点
        HTuple htup_NumMeasureCircle = 100;
        HTuple htup_theradd_leftup_measure_cricle = htup_AngleTopLeft / (htup_NumMeasureCircle + 2);
        HTuple htup_length1 = 300;
        HTuple htup_length2 = 1;
        HTuple Num = 0, htup_ther, htup_row1, htup_col1;
        HTuple MsrHandle_Measure_01_0 = null, LengthMeasure = null;
        HTuple htup_Row_Measure = null, htup_Column_Measure = null, htup_Amplitude_Measure = null, htup_Distance_Measure = null, htup_Length0 = 0;
        for (int Index = 0; Index < htup_NumMeasureCircle; Index++)
        {
            htup_ther = (((90 + htup_AngleLeftY) + (htup_theradd_leftup_measure_cricle * (Index + 1)))).TupleRad()
                ;
            htup_row1 = htup_TopLeftRow + (htup_length1 * (htup_ther.TupleSin()));
            htup_col1 = htup_TopLeftCol - (htup_length1 * (htup_ther.TupleCos()));
            //
            HOperatorSet.GenMeasureRectangle2(htup_row1, htup_col1, htup_ther, htup_length1, htup_length2,
                this.htup_ImgWidth, this.htup_ImgHeight, "nearest_neighbor", out MsrHandle_Measure_01_0);
            HOperatorSet.MeasurePos(hobj_OrgImg, MsrHandle_Measure_01_0, 2.9, 30, "positive",
                "all", out htup_Row_Measure, out htup_Column_Measure, out htup_Amplitude_Measure,
                out htup_Distance_Measure);
            HOperatorSet.TupleLength(htup_Row_Measure, out LengthMeasure);
            if ((int)(new HTuple(LengthMeasure.TupleGreater(0))) != 0)
            {
                if (htup_TempRowEdgelu == null)
                    htup_TempRowEdgelu = new HTuple();
                htup_TempRowEdgelu[Num] = htup_Row_Measure.TupleSelect(0);
                if (htup_TempColumnEdgelu == null)
                    htup_TempColumnEdgelu = new HTuple();
                htup_TempColumnEdgelu[Num] = htup_Column_Measure.TupleSelect(0);
                Num = Num + 1;
            }
            HOperatorSet.CloseMeasure(MsrHandle_Measure_01_0);
        }







        //*************************************右上弧的点
        HTuple htup_theradd_rightup_measure_cricle = htup_AngleTopRight / (htup_NumMeasureCircle + 2);
        htup_length1 = 300;
        htup_length2 = 1;
        Num = 0;
        for (int Index = 0; Index < htup_NumMeasureCircle; Index++)
        {
            htup_ther = (((90 + htup_AngleRightY) - (htup_theradd_rightup_measure_cricle * (Index + 1)))).TupleRad()
                ;
            htup_row1 = htup_TopRightRow + (htup_length1 * (htup_ther.TupleSin()));
            htup_col1 = htup_ToprightCol - (htup_length1 * (htup_ther.TupleCos()));
            HOperatorSet.GenMeasureRectangle2(htup_row1, htup_col1, htup_ther, htup_length1, htup_length2,
                this.htup_ImgWidth, this.htup_ImgHeight, "nearest_neighbor", out MsrHandle_Measure_01_0);
            HOperatorSet.MeasurePos(hobj_OrgImg, MsrHandle_Measure_01_0, 2.9, 30, "all",
                "all", out htup_Row_Measure, out htup_Column_Measure, out htup_Amplitude_Measure,
                out htup_Distance_Measure);
            HOperatorSet.TupleLength(htup_Row_Measure, out LengthMeasure);
            if ((int)(new HTuple(LengthMeasure.TupleGreater(0))) != 0)
            {
                if (htup_TempRowEdgeru == null)
                    htup_TempRowEdgeru = new HTuple();
                htup_TempRowEdgeru[Num] = htup_Row_Measure.TupleSelect(0);
                if (htup_TempColumnEdgeru == null)
                    htup_TempColumnEdgeru = new HTuple();
                htup_TempColumnEdgeru[Num] = htup_Column_Measure.TupleSelect(0);
                Num = Num + 1;
            }
            HOperatorSet.CloseMeasure(MsrHandle_Measure_01_0);
        }






        //*************************************左下弧的点
        HTuple htup_theradd_leftdown_measure_cricle = htup_AngleBottomLeft / (htup_NumMeasureCircle + 2);
        htup_length1 = 300;
        htup_length2 = 1;
        htup_TempRowEdgeld = new HTuple();
        htup_TempColumnEdgeld = new HTuple();
        Num = 0;

        for (int Index = 0; Index < htup_NumMeasureCircle; Index++)
        {
            htup_Row_Measure = new HTuple();
            htup_Column_Measure = new HTuple();
            htup_ther = ((htup_AngleBottomX + (htup_theradd_leftdown_measure_cricle * (Index + 1)))).TupleRad()
                ;
            htup_row1 = htup_BottomLeftRow - (htup_length1 * (htup_ther.TupleSin()));
            htup_col1 = htup_BottomLeftCol + (htup_length1 * (htup_ther.TupleCos()));
            //
            HOperatorSet.GenMeasureRectangle2(htup_row1, htup_col1, htup_ther, htup_length1, htup_length2,
                this.htup_ImgWidth, this.htup_ImgHeight, "nearest_neighbor", out MsrHandle_Measure_01_0);
            HOperatorSet.MeasurePos(hobj_OrgImg, MsrHandle_Measure_01_0, 2.9, 30, "all",
                "all", out htup_Row_Measure, out htup_Column_Measure, out htup_Amplitude_Measure,
                out htup_Distance_Measure);
            HOperatorSet.TupleLength(htup_Row_Measure, out LengthMeasure);
            if ((int)(new HTuple(LengthMeasure.TupleGreater(0))) != 0)
            {
                if (htup_TempRowEdgeld == null)
                    htup_TempRowEdgeld = new HTuple();
                htup_TempRowEdgeld[Num] = htup_Row_Measure.TupleSelect(0);
                if (htup_TempColumnEdgeld == null)
                    htup_TempColumnEdgeld = new HTuple();
                htup_TempColumnEdgeld[Num] = htup_Column_Measure.TupleSelect(0);
                //
                Num = Num + 1;
            }
            HOperatorSet.CloseMeasure(MsrHandle_Measure_01_0);
        }



        //*************************************右下弧的点
        HTuple htup_theradd_rightdown_measure_cricle = htup_AngleBottomRight / (htup_NumMeasureCircle + 2);
        htup_length1 = 300;
        htup_length2 = 1;
        Num = 0;

        for (int Index = 0; Index < htup_NumMeasureCircle; Index++)
        {
            htup_ther = (((90 + htup_AngleRightY) + (htup_theradd_rightdown_measure_cricle * (Index + 1)))).TupleRad()
                ;
            htup_row1 = htup_BottomRightRow - (htup_length1 * (htup_ther.TupleSin()));
            htup_col1 = htup_BottomRightCol + (htup_length1 * (htup_ther.TupleCos()));
            //
            HOperatorSet.GenMeasureRectangle2(htup_row1, htup_col1, htup_ther, htup_length1, htup_length2,
                this.htup_ImgWidth, this.htup_ImgHeight, "nearest_neighbor", out MsrHandle_Measure_01_0);
            HOperatorSet.MeasurePos(hobj_OrgImg, MsrHandle_Measure_01_0, 2.9, 30, "all",
                "all", out htup_Row_Measure, out htup_Column_Measure, out htup_Amplitude_Measure,
                out htup_Distance_Measure);
            HOperatorSet.TupleLength(htup_Row_Measure, out LengthMeasure);
            if ((int)(new HTuple(LengthMeasure.TupleGreater(0))) != 0)
            {
                if (htup_TempRowEdgerd == null)
                    htup_TempRowEdgerd = new HTuple();
                htup_TempRowEdgerd[Num] = htup_Row_Measure.TupleSelect(0);
                if (htup_TempColumnEdgerd == null)
                    htup_TempColumnEdgerd = new HTuple();
                htup_TempColumnEdgerd[Num] = htup_Column_Measure.TupleSelect(0);
                Num = Num + 1;
            }
            HOperatorSet.CloseMeasure(MsrHandle_Measure_01_0);
            //
        }

    }
    //********************************求4个弧上的点拟合圆并且求出八个交点*********************************
    public void GetArcEdgePtToCricle()
    {
        HTuple htup_StartPhilu, htup_EndPhilu, htup_PointOrder,
                htup_StartPhiru, htup_EndPhiru, htup_PointOrderru,
                htup_StartPhild, htup_EndPhild, htup_PointOrderld,
                htup_StartPhird, htup_EndPhird, htup_PointOrderrd,
                htup_StartPhilurd, htup_EndPhilurd, htup_PointOrderlurd,
                htup_StartPhildru, htup_EndPhildru, htup_PointOrderldru,
                htup_StartPhiall, htup_EndPhiall, htup_PointOrderall;
        HObject Hobj_Contourlu, Hobj_Contourru, Hobj_Contourld, Hobj_Contourrd,
                Hobj_Contourlurd, Hobj_Contourldru, Hobj_Contourall;
        HTuple htup_TempRowEdgelurd = new HTuple();
        HTuple htup_TempColumnEdgelurd = new HTuple();
        HTuple htup_TempRowEdgeldru = new HTuple();
        HTuple htup_TempColumnEdgeldru = new HTuple();
        HTuple htup_TempRowEdgeall = new HTuple();
        HTuple htup_TempColumnEdgeall = new HTuple();
        HOperatorSet.GenContourPolygonXld(out Hobj_Contourlu, htup_TempRowEdgelu, htup_TempColumnEdgelu);
        HOperatorSet.FitCircleContourXld(Hobj_Contourlu, "geotukey", -1, 0, 0, 5, 2, out htup_Rowlu,
            out htup_Columnlu, out htup_Radiuslu, out htup_StartPhilu, out htup_EndPhilu, out htup_PointOrder);
        HOperatorSet.GenContourPolygonXld(out Hobj_Contourru, htup_TempRowEdgeru, htup_TempColumnEdgeru);
        HOperatorSet.FitCircleContourXld(Hobj_Contourru, "geotukey", -1, 0, 0, 5, 2, out htup_Rowru,
            out htup_Columnru, out htup_Radiusru, out htup_StartPhiru, out htup_EndPhiru, out htup_PointOrderru);
        HOperatorSet.GenContourPolygonXld(out Hobj_Contourld, htup_TempRowEdgeld, htup_TempColumnEdgeld);
        HOperatorSet.FitCircleContourXld(Hobj_Contourld, "geotukey", -1, 0, 0, 5, 2, out htup_Rowld,
            out htup_Columnld, out htup_Radiusld, out htup_StartPhild, out htup_EndPhild, out htup_PointOrderld);
        HOperatorSet.GenContourPolygonXld(out Hobj_Contourrd, htup_TempRowEdgerd, htup_TempColumnEdgerd);
        HOperatorSet.FitCircleContourXld(Hobj_Contourrd, "geotukey", -1, 0, 0, 5, 2, out htup_Rowrd,
            out htup_Columnrd, out htup_Radiusrd, out htup_StartPhird, out htup_EndPhird, out htup_PointOrderrd);
        htup_TempRowEdgelurd = new HTuple();
        htup_TempRowEdgelurd = htup_TempRowEdgelurd.TupleConcat(htup_TempRowEdgelu);
        htup_TempRowEdgelurd = htup_TempRowEdgelurd.TupleConcat(htup_TempRowEdgerd);
        htup_TempColumnEdgelurd = new HTuple();
        htup_TempColumnEdgelurd = htup_TempColumnEdgelurd.TupleConcat(htup_TempColumnEdgelu);
        htup_TempColumnEdgelurd = htup_TempColumnEdgelurd.TupleConcat(htup_TempColumnEdgerd);
        htup_TempRowEdgeldru = new HTuple();
        htup_TempRowEdgeldru = htup_TempRowEdgeldru.TupleConcat(htup_TempRowEdgeld);
        htup_TempRowEdgeldru = htup_TempRowEdgeldru.TupleConcat(htup_TempRowEdgeru);
        htup_TempColumnEdgeldru = new HTuple();
        htup_TempColumnEdgeldru = htup_TempColumnEdgeldru.TupleConcat(htup_TempColumnEdgeld);
        htup_TempColumnEdgeldru = htup_TempColumnEdgeldru.TupleConcat(htup_TempColumnEdgeru);
        htup_TempRowEdgeall = new HTuple();
        htup_TempRowEdgeall = htup_TempRowEdgeall.TupleConcat(htup_TempRowEdgelu);
        htup_TempRowEdgeall = htup_TempRowEdgeall.TupleConcat(htup_TempRowEdgeru);
        htup_TempRowEdgeall = htup_TempRowEdgeall.TupleConcat(htup_TempRowEdgerd);
        htup_TempRowEdgeall = htup_TempRowEdgeall.TupleConcat(htup_TempRowEdgeld);
        htup_TempColumnEdgeall = new HTuple();
        htup_TempColumnEdgeall = htup_TempColumnEdgeall.TupleConcat(htup_TempColumnEdgelu);
        htup_TempColumnEdgeall = htup_TempColumnEdgeall.TupleConcat(htup_TempColumnEdgeru);
        htup_TempColumnEdgeall = htup_TempColumnEdgeall.TupleConcat(htup_TempColumnEdgerd);
        htup_TempColumnEdgeall = htup_TempColumnEdgeall.TupleConcat(htup_TempColumnEdgeld);

        //左上右下拟合成一个圆
        HOperatorSet.GenContourPolygonXld(out Hobj_Contourlurd, htup_TempRowEdgelurd, htup_TempColumnEdgelurd);
        HOperatorSet.FitCircleContourXld(Hobj_Contourlurd, "ahuber", -1, 0, 0, 5, 2, out htup_Rowlurd,
            out htup_Columnlurd, out htup_Radiuslurd, out htup_StartPhilurd, out htup_EndPhilurd,
            out htup_PointOrderlurd);
        //
        //左下右上拟合成一个圆
        HOperatorSet.GenContourPolygonXld(out Hobj_Contourldru, htup_TempRowEdgeldru, htup_TempColumnEdgeldru);
        HOperatorSet.FitCircleContourXld(Hobj_Contourldru, "ahuber", -1, 0, 0, 5, 2, out htup_Rowldru,
            out htup_Columnldru, out htup_Radiusldru, out htup_StartPhildru, out htup_EndPhildru,
            out htup_PointOrderldru);

        //
        //四个角拟合成一个圆
        HOperatorSet.GenContourPolygonXld(out Hobj_Contourall, htup_TempRowEdgeall, htup_TempColumnEdgeall);
        HOperatorSet.FitCircleContourXld(Hobj_Contourall, "ahuber", -1, 0, 0, 5, 2, out htup_Rowall,
            out htup_Columnall, out htup_Radiusall, out htup_StartPhiall, out htup_EndPhiall,
            out htup_PointOrderall);

        //得到拟合圆与直线的交点，即八个角点pt__1~pt_8
        HTuple htup_Row_incert1 = null, htup_Column_incert1 = null;
        HTuple htup_len = null;
        HOperatorSet.IntersectionLineCircle(htup_TopLeftRow, htup_TopLeftCol, htup_BottomLeftRow,
            htup_BottomLeftCol, htup_Rowlurd, htup_Columnlurd, htup_Radiuslurd, 0, 6.28318, "positive",
            out htup_Row_incert1, out htup_Column_incert1);
        HOperatorSet.TupleLength(htup_Row_incert1, out htup_len);
        if ((int)(new HTuple(htup_len.TupleEqual(2))) != 0)
        {
            if ((int)(new HTuple(((htup_Row_incert1.TupleSelect(0))).TupleGreater(htup_Row_incert1.TupleSelect(
                1)))) != 0)
            {
                htup_pt_row_lineleft_up = htup_Row_incert1[1];
                htup_pt_col_lineleft_up = htup_Column_incert1[1];
            }
            else
            {
                htup_pt_row_lineleft_up = htup_Row_incert1[0];
                htup_pt_col_lineleft_up = htup_Column_incert1[0];
            }
        }
        HTuple htup_Row_incert2 = null, htup_Column_incert2 = null;

        HOperatorSet.IntersectionLineCircle(htup_TopLeftRow, htup_TopLeftCol, htup_TopRightRow,
            htup_ToprightCol, htup_Rowlurd, htup_Columnlurd, htup_Radiuslurd, 0, 6.28318, "positive",
            out htup_Row_incert2, out htup_Column_incert2);
        HOperatorSet.TupleLength(htup_Column_incert2, out htup_len);
        if ((int)(new HTuple(htup_len.TupleEqual(2))) != 0)
        {
            if ((int)(new HTuple(((htup_Column_incert2.TupleSelect(0))).TupleGreater(htup_Column_incert2.TupleSelect(
                1)))) != 0)
            {
                htup_pt_row_lineup_left = htup_Row_incert2[1];
                htup_pt_col_lineup_left = htup_Column_incert2[1];
            }
            else
            {
                htup_pt_row_lineup_left = htup_Row_incert2[0];
                htup_pt_col_lineup_left = htup_Column_incert2[0];
            }
        }
        //
        HTuple htup_Row_incert3 = null, htup_Column_incert3 = null;
        HOperatorSet.IntersectionLineCircle(htup_TopLeftRow, htup_TopLeftCol, htup_TopRightRow,
            htup_ToprightCol, htup_Rowldru, htup_Columnldru, htup_Radiusldru, 0, 6.28318, "positive",
            out htup_Row_incert3, out htup_Column_incert3);
        HOperatorSet.TupleLength(htup_Column_incert3, out htup_len);
        if ((int)(new HTuple(htup_len.TupleEqual(2))) != 0)
        {
            if ((int)(new HTuple(((htup_Column_incert3.TupleSelect(0))).TupleGreater(htup_Column_incert3.TupleSelect(
                1)))) != 0)
            {
                htup_pt_row_lineup_right = htup_Row_incert3[0];
                htup_pt_col_lineup_right = htup_Column_incert3[0];
            }
            else
            {
                htup_pt_row_lineup_right = htup_Row_incert3[1];
                htup_pt_col_lineup_right = htup_Column_incert3[1];
            }
        }
        //
        HTuple htup_Row_incert4 = null, htup_Column_incert4 = null;
        HOperatorSet.IntersectionLineCircle(htup_TopRightRow, htup_ToprightCol, htup_BottomRightRow,
            htup_BottomRightCol, htup_Rowldru, htup_Columnldru, htup_Radiusldru, 0, 6.28318, "positive",
            out htup_Row_incert4, out htup_Column_incert4);
        HOperatorSet.TupleLength(htup_Row_incert4, out htup_len);
        if ((int)(new HTuple(htup_len.TupleEqual(2))) != 0)
        {
            if ((int)(new HTuple(((htup_Row_incert4.TupleSelect(0))).TupleGreater(htup_Row_incert4.TupleSelect(
                1)))) != 0)
            {
                htup_pt_row_lineright_up = htup_Row_incert4[1];
                htup_pt_col_lineright_up = htup_Column_incert4[1];
            }
            else
            {
                htup_pt_row_lineright_up = htup_Row_incert4[0];
                htup_pt_col_lineright_up = htup_Column_incert4[0];
            }
        }
        HTuple htup_Row_incert5 = null, htup_Column_incert5 = null;
        HOperatorSet.IntersectionLineCircle(htup_TopRightRow, htup_ToprightCol, htup_BottomRightRow,
            htup_BottomRightCol, htup_Rowlurd, htup_Columnlurd, htup_Radiuslurd, 0, 6.28318, "positive",
            out htup_Row_incert5, out htup_Column_incert5);
        HOperatorSet.TupleLength(htup_Row_incert5, out htup_len);
        if ((int)(new HTuple(htup_len.TupleEqual(2))) != 0)
        {
            if ((int)(new HTuple(((htup_Row_incert5.TupleSelect(0))).TupleGreater(htup_Row_incert5.TupleSelect(
                1)))) != 0)
            {
                htup_pt_row_lineright_down = htup_Row_incert5[0];
                htup_pt_col_lineright_down = htup_Column_incert5[0];
            }
            else
            {
                htup_pt_row_lineright_down = htup_Row_incert5[1];
                htup_pt_col_lineright_down = htup_Column_incert5[1];
            }
        }

        HTuple htup_Row_incert6 = null, htup_Column_incert6 = null;
        HOperatorSet.IntersectionLineCircle(htup_BottomLeftRow, htup_BottomLeftCol, htup_BottomRightRow,
            htup_BottomRightCol, htup_Rowlurd, htup_Columnlurd, htup_Radiuslurd, 0, 6.28318, "positive",
            out htup_Row_incert6, out htup_Column_incert6);
        HOperatorSet.TupleLength(htup_Row_incert6, out htup_len);
        if ((int)(new HTuple(htup_len.TupleEqual(2))) != 0)
        {
            if ((int)(new HTuple(((htup_Column_incert6.TupleSelect(0))).TupleGreater(htup_Column_incert6.TupleSelect(
                1)))) != 0)
            {
                htup_pt_row_linedown_right = htup_Row_incert6[0];
                htup_pt_col_linedown_right = htup_Column_incert6[0];
            }
            else
            {
                htup_pt_row_linedown_right = htup_Row_incert6[1];
                htup_pt_col_linedown_right = htup_Column_incert6[1];
            }
        }
        HTuple htup_Row_incert7 = null, htup_Column_incert7 = null;
        HOperatorSet.IntersectionLineCircle(htup_BottomLeftRow, htup_BottomLeftCol, htup_BottomRightRow,
            htup_BottomRightCol, htup_Rowldru, htup_Columnldru, htup_Radiusldru, 0, 6.28318, "positive",
            out htup_Row_incert7, out htup_Column_incert7);
        HOperatorSet.TupleLength(htup_Row_incert7, out htup_len);
        if ((int)(new HTuple(htup_len.TupleEqual(2))) != 0)
        {
            if ((int)(new HTuple(((htup_Column_incert7.TupleSelect(0))).TupleGreater(htup_Column_incert7.TupleSelect(
                1)))) != 0)
            {
                htup_pt_row_linedown_left = htup_Row_incert7[1];
                htup_pt_col_linedown_left = htup_Column_incert7[1];
            }
            else
            {
                htup_pt_row_linedown_left = htup_Row_incert7[0];
                htup_pt_col_linedown_left = htup_Column_incert7[0];
            }
        }
        HTuple htup_Row_incert8, htup_Column_incert8;
        HOperatorSet.IntersectionLineCircle(htup_TopLeftRow, htup_TopLeftCol, htup_BottomLeftRow,
            htup_BottomLeftCol, htup_Rowldru, htup_Columnldru, htup_Radiusldru, 0, 6.28318, "positive",
            out htup_Row_incert8, out htup_Column_incert8);
        HOperatorSet.TupleLength(htup_Row_incert8, out htup_len);
        if ((int)(new HTuple(htup_len.TupleEqual(2))) != 0)
        {
            if ((int)(new HTuple(((htup_Row_incert8.TupleSelect(0))).TupleGreater(htup_Row_incert8.TupleSelect(
                1)))) != 0)
            {
                htup_pt_row_lineleft_down = htup_Row_incert8[0];
                htup_pt_col_lineleft_down = htup_Column_incert8[0];
            }
            else
            {
                htup_pt_row_lineleft_down = htup_Row_incert8[1];
                htup_pt_col_lineleft_down = htup_Column_incert8[1];
            }
        }

    }
    //********************************求4个弧上的点拟合直线并且求出八个交点*******************************
    public void GetArcEdgePtToLine()
    {

        HObject Hobj_Contourlineleftup, Hobj_Contourlineleftdown, Hobj_Contourrightup, Hobj_Contourrightdown;
        HTuple htup_TopLeftRowBegin, htup_ColleftupBegin, htup_TopLeftRowEnd, htup_ColleftupEnd;
        HTuple htup_Nr1, htup_Nc1, htup_Dist1;
        //
        //
        //**********************************拟合四个角的线
        //
        //**********************************lineleftup
        HOperatorSet.GenContourPolygonXld(out Hobj_Contourlineleftup, htup_TempRowEdgelu,
              htup_TempColumnEdgelu);
        HOperatorSet.FitLineContourXld(Hobj_Contourlineleftup, "tukey", -1, 0, 5, 2, out htup_TopLeftRowBegin,
            out htup_ColleftupBegin, out htup_TopLeftRowEnd, out htup_ColleftupEnd, out htup_Nr1,
            out htup_Nc1, out htup_Dist1);

        HTuple htup_BottomLeftRowBegin, htup_ColleftdownBegin, htup_BottomLeftRowEnd, htup_ColleftdownEnd;
        //**********************************lineleftdown
        HOperatorSet.GenContourPolygonXld(out Hobj_Contourlineleftdown, htup_TempRowEdgeld,
             htup_TempColumnEdgeld);
        HOperatorSet.FitLineContourXld(Hobj_Contourlineleftdown, "tukey", -1, 0, 5, 2,
            out htup_BottomLeftRowBegin, out htup_ColleftdownBegin, out htup_BottomLeftRowEnd,
            out htup_ColleftdownEnd, out htup_Nr1, out htup_Nc1, out htup_Dist1);

        HTuple htup_TopRightRowBegin, htup_ColrightupBegin, htup_TopRightRowEnd, htup_ColrightupEnd;
        //**********************************linerightup
        HOperatorSet.GenContourPolygonXld(out Hobj_Contourrightup, htup_TempRowEdgeru, htup_TempColumnEdgeru);
        HOperatorSet.FitLineContourXld(Hobj_Contourrightup, "tukey", -1, 0, 5, 2, out htup_TopRightRowBegin,
            out htup_ColrightupBegin, out htup_TopRightRowEnd, out htup_ColrightupEnd, out htup_Nr1,
            out htup_Nc1, out htup_Dist1);

        //**********************************linerightdown
        HTuple htup_BottomRightRowBegin, htup_ColrightdownBegin, htup_BottomRightRowEnd, htup_ColrightdownEnd;
        HOperatorSet.GenContourPolygonXld(out Hobj_Contourrightdown, htup_TempRowEdgerd,
             htup_TempColumnEdgerd);
        HOperatorSet.FitLineContourXld(Hobj_Contourrightdown, "tukey", -1, 0, 5, 2, out htup_BottomRightRowBegin,
            out htup_ColrightdownBegin, out htup_BottomRightRowEnd, out htup_ColrightdownEnd,
            out htup_Nr1, out htup_Nc1, out htup_Dist1);


        HTuple htup_IsOverlapping;
        //得到八个角点
        HOperatorSet.IntersectionLines(htup_TopLeftRowBegin, htup_ColleftupBegin, htup_TopLeftRowEnd,
            htup_ColleftupEnd, htup_TopLeftRow, htup_TopLeftCol, htup_BottomLeftRow, htup_BottomLeftCol,
            out htup_pt_row_lineleft_up, out htup_pt_col_lineleft_up, out htup_IsOverlapping);
        HOperatorSet.IntersectionLines(htup_TopLeftRowBegin, htup_ColleftupBegin, htup_TopLeftRowEnd,
            htup_ColleftupEnd, htup_TopLeftRow, htup_TopLeftCol, htup_TopRightRow, htup_ToprightCol,
            out htup_pt_row_lineup_left, out htup_pt_col_lineup_left, out htup_IsOverlapping);
        HOperatorSet.IntersectionLines(htup_TopRightRowBegin, htup_ColrightupBegin, htup_TopRightRowEnd,
            htup_ColrightupEnd, htup_TopLeftRow, htup_TopLeftCol, htup_TopRightRow, htup_ToprightCol,
            out htup_pt_row_lineup_right, out htup_pt_col_lineup_right, out htup_IsOverlapping);
        HOperatorSet.IntersectionLines(htup_TopRightRowBegin, htup_ColrightupBegin, htup_TopRightRowEnd,
            htup_ColrightupEnd, htup_TopRightRow, htup_ToprightCol, htup_BottomRightRow, htup_BottomRightCol,
            out htup_pt_row_lineright_up, out htup_pt_col_lineright_up, out htup_IsOverlapping);
        HOperatorSet.IntersectionLines(htup_BottomRightRowBegin, htup_ColrightdownBegin, htup_BottomRightRowEnd,
            htup_ColrightdownEnd, htup_TopRightRow, htup_ToprightCol, htup_BottomRightRow, htup_BottomRightCol,
            out htup_pt_row_lineright_down, out htup_pt_col_lineright_down, out htup_IsOverlapping);
        HOperatorSet.IntersectionLines(htup_BottomRightRowBegin, htup_ColrightdownBegin, htup_BottomRightRowEnd,
            htup_ColrightdownEnd, htup_BottomLeftRow, htup_BottomLeftCol, htup_BottomRightRow, htup_BottomRightCol,
            out htup_pt_row_linedown_right, out htup_pt_col_linedown_right, out htup_IsOverlapping);
        HOperatorSet.IntersectionLines(htup_BottomLeftRowBegin, htup_ColleftdownBegin, htup_BottomLeftRowEnd,
            htup_ColleftdownEnd, htup_BottomLeftRow, htup_BottomLeftCol, htup_BottomRightRow, htup_BottomRightCol,
            out htup_pt_row_linedown_left, out htup_pt_col_linedown_left, out htup_IsOverlapping);
        HOperatorSet.IntersectionLines(htup_BottomLeftRowBegin, htup_ColleftdownBegin, htup_BottomLeftRowEnd,
            htup_ColleftdownEnd, htup_TopLeftRow, htup_TopLeftCol, htup_BottomLeftRow, htup_BottomLeftCol,
            out htup_pt_row_lineleft_down, out htup_pt_col_lineleft_down, out htup_IsOverlapping);
    }
   //********************************获取单晶的电池片roi**************************************************
    public void GetMonocrystalROI()
    {
        HTuple htup_roiadd = 2;
        HTuple htup_ROI_row = new HTuple();
        htup_ROI_row[0] = htup_TopLeftRow;
        htup_ROI_row[1] = htup_TopRightRow;
        htup_ROI_row[2] = htup_BottomRightRow;
        htup_ROI_row[3] = htup_BottomLeftRow;
        HTuple htup_ROI_col = new HTuple();
        htup_ROI_col[0] = htup_TopLeftCol;
        htup_ROI_col[1] = htup_ToprightCol;
        htup_ROI_col[2] = htup_BottomRightCol;
        htup_ROI_col[3] = htup_BottomLeftCol;

        HOperatorSet.GenRegionPolygonFilled(out hobj_WholeROI, htup_ROI_row, htup_ROI_col);
        HObject hobj_Circle;
        HOperatorSet.GenEmptyObj(out hobj_Circle);
        hobj_Circle.Dispose();
        HOperatorSet.GenCircle(out hobj_Circle, htup_Rowall, htup_Columnall, htup_Radiusall - htup_roiadd);//四个弧拟合的圆

        {//求交集得到完整Roi
            HObject hobj_TmpRoi;
            HOperatorSet.GenEmptyObj(out hobj_TmpRoi);
            HOperatorSet.Intersection(hobj_Circle, hobj_WholeROI, out hobj_TmpRoi);
            hobj_WholeROI.Dispose();
            hobj_WholeROI = hobj_TmpRoi;
        }  
    }
    //********************************获取多晶的电池片roi********************************* 
    public void GetPolycrystalROI()
    {
        HTuple htup_roiadd = 2;
        HTuple htup_ROI_row = new HTuple();
        htup_ROI_row[0] = htup_pt_row_lineleft_up;
        htup_ROI_row[1] = htup_pt_row_lineup_left;
        htup_ROI_row[2] = htup_pt_row_lineup_right;
        htup_ROI_row[3] = htup_pt_row_lineright_up;
        htup_ROI_row[4] = htup_pt_row_lineright_down;
        htup_ROI_row[5] = htup_pt_row_linedown_right;
        htup_ROI_row[6] = htup_pt_row_linedown_left;
        htup_ROI_row[7] = htup_pt_row_lineleft_down;
        HTuple htup_ROI_col = new HTuple();
        htup_ROI_col[0] = htup_pt_col_lineleft_up;
        htup_ROI_col[1] = htup_pt_col_lineup_left;
        htup_ROI_col[2] = htup_pt_col_lineup_right;
        htup_ROI_col[3] = htup_pt_col_lineright_up;
        htup_ROI_col[4] = htup_pt_col_lineright_down;
        htup_ROI_col[5] = htup_pt_col_linedown_right;
        htup_ROI_col[6] = htup_pt_col_linedown_left;
        htup_ROI_col[7] = htup_pt_col_lineleft_down;
        HOperatorSet.GenRegionPolygonFilled(out hobj_WholeROI, htup_ROI_row, htup_ROI_col);
    }
    //********************************孔洞检查*****************************
    public void HoleDetect()
    { 
        HObject hobj_ImgOfRoi;
        HTuple htup_HoleMinArea=25;
        HOperatorSet.GenEmptyObj(out hobj_ImgOfRoi);
        hobj_ImgOfRoi.Dispose();
        HOperatorSet.ReduceDomain(hobj_OrgImg, hobj_WholeROI, out hobj_ImgOfRoi);
        HObject hobj_RegionOfRoi;
        HOperatorSet.GenEmptyObj(out hobj_RegionOfRoi);
        hobj_RegionOfRoi.Dispose();
        HOperatorSet.Threshold(hobj_ImgOfRoi, out hobj_RegionOfRoi, htup_Meanbackgroud - AmplitudeThreshold,
            255);
        HObject hobj_ConnectedRegionOfRoi;
        HOperatorSet.GenEmptyObj(out hobj_ConnectedRegionOfRoi);
        hobj_ConnectedRegionOfRoi.Dispose();
        HOperatorSet.Connection(hobj_RegionOfRoi, out hobj_ConnectedRegionOfRoi);
        HTuple htup_selectnumber = null;
        htup_selectnumber = htup_HoleMinArea;//孔洞最小值设置
        HObject hobj_SelectedRegions = null;HOperatorSet.GenEmptyObj(out hobj_SelectedRegions);
        hobj_SelectedRegions.Dispose();
        HOperatorSet.SelectShape(hobj_ConnectedRegionOfRoi, out hobj_SelectedRegions, "area",
            "and", htup_selectnumber, 25000000);
        HTuple htup_Number;
        HOperatorSet.CountObj(hobj_SelectedRegions, out htup_Number);//找到所有连通区域的个数
        
        htup_hole_count = 0;//找到所有孔洞的个数
        HTuple htup_countselect;
        htup_countselect = new HTuple();//存放region的下标
        HTuple htup_distanceline;
        htup_distanceline = 3;//到边线的距离 
        //排除检查孔洞时的电池片边缘的干扰
        HTuple end_val651 = htup_Number;
        HTuple step_val651 = 1;
        HTuple htup_i=null,htup_Area, htup_Rowselect, htup_Columnselect, htup_Distanceleft,
            htup_Distanceup, htup_Distanceright, htup_Distancedown;
       
        HObject hobj_RegionSelected;
        HOperatorSet.GenEmptyObj(out hobj_RegionSelected);
        for (htup_i = 1; htup_i.Continue(end_val651, step_val651); htup_i = htup_i.TupleAdd(step_val651))
        {
            hobj_RegionSelected.Dispose();
            HOperatorSet.SelectObj(hobj_SelectedRegions, out hobj_RegionSelected, htup_i);
            HOperatorSet.AreaCenter(hobj_RegionSelected, out htup_Area, out htup_Rowselect, out htup_Columnselect);
            HOperatorSet.DistancePl(htup_Rowselect, htup_Columnselect, htup_pt_row_lineleft_up,
                htup_pt_col_lineleft_up, htup_pt_row_lineleft_down, htup_pt_col_lineleft_down,
                out htup_Distanceleft);
            if ((int)(new HTuple(htup_Distanceleft.TupleLess(htup_distanceline))) != 0)
            {
            }
            else
            {
                HOperatorSet.DistancePl(htup_Rowselect, htup_Columnselect, htup_pt_row_lineup_left,
                    htup_pt_col_lineup_left, htup_pt_row_lineup_right, htup_pt_col_lineup_right,
                    out htup_Distanceup);
                if ((int)(new HTuple(htup_Distanceup.TupleLess(htup_distanceline))) != 0)
                {
                }
                else
                {
                    HOperatorSet.DistancePl(htup_Rowselect, htup_Columnselect, htup_pt_row_lineright_up,
                        htup_pt_col_lineright_up, htup_pt_row_lineright_down, htup_pt_col_lineright_down,
                        out htup_Distanceright);
                    if ((int)(new HTuple(htup_Distanceright.TupleLess(htup_distanceline))) != 0)
                    {
                    }
                    else
                    {
                        HOperatorSet.DistancePl(htup_Rowselect, htup_Columnselect, htup_pt_row_linedown_right,
                            htup_pt_col_linedown_right, htup_pt_row_linedown_left, htup_pt_col_linedown_left,
                            out htup_Distancedown);
                        if ((int)(new HTuple(htup_Distancedown.TupleLess(htup_distanceline))) != 0)
                        {
                        }
                        else
                        {
                            //dev_display (RegionSelected)
                            if (htup_countselect == null)
                                htup_countselect = new HTuple();
                            htup_countselect[htup_hole_count] = htup_i;
                            htup_hole_count = htup_hole_count + 1;
                        }
                    }
                }
            }
        }



    }
    //*************************显示一些检测结果*****************************
    public void DisplayResult()
    {
        //*****************显示角点*********************
        HObject hobj_TopLeft, hobj_TopRight, hobj_BottomLeft, hobj_BottomRight;
        HOperatorSet.GenEmptyObj(out hobj_TopLeft);
        HOperatorSet.GenEmptyObj(out hobj_TopRight);
        HOperatorSet.GenEmptyObj(out hobj_BottomLeft);
        HOperatorSet.GenEmptyObj(out hobj_BottomRight);
        HOperatorSet.GenCrossContourXld(out hobj_TopLeft, htup_TopLeftRow, htup_TopLeftCol, 100, 0);
        HOperatorSet.GenCrossContourXld(out hobj_TopRight, htup_TopRightRow, htup_ToprightCol, 100, 0);
        HOperatorSet.GenCrossContourXld(out hobj_BottomLeft, htup_BottomLeftRow, htup_BottomLeftCol, 100, 0);
        HOperatorSet.GenCrossContourXld(out hobj_BottomRight, htup_BottomRightRow, htup_BottomRightCol, 100, 0);

        HOperatorSet.DispObj(hobj_TopLeft, WinHandle);
        HOperatorSet.DispObj(hobj_TopRight, WinHandle);
        HOperatorSet.DispObj(hobj_BottomLeft, WinHandle);
        HOperatorSet.DispObj(hobj_BottomRight, WinHandle);
        //***************显示对角线*******************
        HObject hobj_linediagonal1, hobj_linediagonal2, hobj_CrossCenter;
        HOperatorSet.GenEmptyObj(out hobj_linediagonal1);
        HOperatorSet.GenEmptyObj(out hobj_linediagonal2);
        HOperatorSet.GenEmptyObj(out hobj_CrossCenter);
        HOperatorSet.GenRegionLine(out hobj_linediagonal1, htup_TopLeftRow, htup_TopLeftCol,
        htup_BottomRightRow, htup_BottomRightCol);
        HOperatorSet.GenRegionLine(out hobj_linediagonal2, htup_TopRightRow, htup_ToprightCol,
        htup_BottomLeftRow, htup_BottomLeftCol);
        HOperatorSet.GenCrossContourXld(out hobj_CrossCenter, htup_centerRow, htup_centerColumn,
          100, 0);
        HOperatorSet.DispObj(hobj_linediagonal1, WinHandle);
        HOperatorSet.DispObj(hobj_linediagonal2, WinHandle);
        HOperatorSet.DispObj(hobj_CrossCenter, WinHandle);
        //----------------***显示八个定位点***----------------
        HObject hobj_Cross1, hobj_Cross2, hobj_Cross3, hobj_Cross4, hobj_Cross5, hobj_Cross6, hobj_Cross7, hobj_Cross8;
        HOperatorSet.GenEmptyObj(out hobj_Cross1); HOperatorSet.GenEmptyObj(out hobj_Cross2);
        HOperatorSet.GenEmptyObj(out hobj_Cross3); HOperatorSet.GenEmptyObj(out hobj_Cross4);
        HOperatorSet.GenEmptyObj(out hobj_Cross5); HOperatorSet.GenEmptyObj(out hobj_Cross6);
        HOperatorSet.GenEmptyObj(out hobj_Cross7); HOperatorSet.GenEmptyObj(out hobj_Cross8);
        HOperatorSet.GenCrossContourXld(out hobj_Cross1, htup_pt_row_lineleft_up, htup_pt_col_lineleft_up,
       100, 0.785398);
        HOperatorSet.GenCrossContourXld(out hobj_Cross2, htup_pt_row_lineup_left, htup_pt_col_lineup_left,
       100, 0.785398);
        HOperatorSet.GenCrossContourXld(out hobj_Cross3, htup_pt_row_lineup_right, htup_pt_col_lineup_right,
       100, 0.785398);
        HOperatorSet.GenCrossContourXld(out hobj_Cross4, htup_pt_row_lineright_up, htup_pt_col_lineright_up,
       100, 0.785398);
        HOperatorSet.GenCrossContourXld(out hobj_Cross5, htup_pt_row_lineright_down, htup_pt_col_lineright_down,
       100, 0.785398);
        HOperatorSet.GenCrossContourXld(out hobj_Cross6, htup_pt_row_linedown_right, htup_pt_col_linedown_right,
       100, 0.785398);
        HOperatorSet.GenCrossContourXld(out hobj_Cross7, htup_pt_row_linedown_left, htup_pt_col_linedown_left,
       100, 0.785398);
        HOperatorSet.GenCrossContourXld(out hobj_Cross8, htup_pt_row_lineleft_down, htup_pt_col_lineleft_down,
       100, 0.785398);
        HOperatorSet.SetColor(WinHandle, "blue");
        HOperatorSet.DispObj(hobj_Cross1, WinHandle); HOperatorSet.DispObj(hobj_Cross2, WinHandle);
        HOperatorSet.DispObj(hobj_Cross3, WinHandle); HOperatorSet.DispObj(hobj_Cross4, WinHandle);
        HOperatorSet.DispObj(hobj_Cross5, WinHandle); HOperatorSet.DispObj(hobj_Cross6, WinHandle);
        HOperatorSet.DispObj(hobj_Cross7, WinHandle); HOperatorSet.DispObj(hobj_Cross8, WinHandle);
        //***************显示拟合圆************************
        //HObject hobj_ContCircleall;
        //HOperatorSet.GenEmptyObj(out hobj_ContCircleall);
        //hobj_ContCircleall.Dispose();
        //HOperatorSet.GenCircleContourXld(out hobj_ContCircleall, htup_Rowall, htup_Columnall,
        //htup_Radiusall, 0, 6.28318, "positive", 1);
        //HOperatorSet.SetColor(WinHandle, "pink");
        //HOperatorSet.DispObj(hobj_ContCircleall, WinHandle);
        //显示全部ROI
        HOperatorSet.SetDraw(WinHandle, "margin");
        HOperatorSet.SetColor(WinHandle, "red");
        HOperatorSet.DispObj(hobj_WholeROI, WinHandle);
    }
    //********************************计算单晶硅参数************************
    public void CalMonocrystalParamater()
    {
        //**************************总边长
        //
        htup_Distance_upline = null; htup_Distance_rightline = null; htup_Distance_leftline = null; htup_Distance_downline = null;
        HOperatorSet.DistancePp(htup_TopLeftRow, htup_TopLeftCol, htup_TopRightRow, htup_ToprightCol,
            out htup_Distance_upline);
        HOperatorSet.DistancePp(htup_TopRightRow, htup_ToprightCol, htup_BottomRightRow, htup_BottomRightCol,
            out htup_Distance_rightline);
        HOperatorSet.DistancePp(htup_TopLeftRow, htup_TopLeftCol, htup_BottomLeftRow, htup_BottomLeftCol,
            out htup_Distance_leftline);
        HOperatorSet.DistancePp(htup_BottomLeftRow, htup_BottomLeftCol, htup_BottomRightRow, htup_BottomRightCol,
            out htup_Distance_downline);
        //**************************边长
        //htup_Distance_topside = null; htup_Distance_leftside = null; htup_Distance_bottomside = null; htup_Distance_rightside = null;
        //HOperatorSet.DistancePp(htup_pt_row_lineup_left, htup_pt_col_lineup_left, htup_pt_row_lineup_right,
        //    htup_pt_col_lineup_right, out htup_Distance_topside);
        //HOperatorSet.DistancePp(htup_pt_row_lineleft_up, htup_pt_col_lineleft_up, htup_pt_row_lineleft_down,
        //    htup_pt_col_lineleft_down, out htup_Distance_leftside);
        //HOperatorSet.DistancePp(htup_pt_row_linedown_left, htup_pt_col_linedown_left, htup_pt_row_linedown_right,
        //    htup_pt_col_linedown_right, out htup_Distance_bottomside);
        //HOperatorSet.DistancePp(htup_pt_row_lineright_up, htup_pt_col_lineright_up, htup_pt_row_lineright_down,
        //    htup_pt_col_lineright_down, out htup_Distance_rightside);
        //**************************两边距
        HTuple htup_Distance_updown1 = null, htup_Distance_updown2 = null, htup_Distance_updown3 = null, htup_Distance_updown4 = null; htup_Distance_updown = null;
        HTuple htup_Distance_leftright1 = null, htup_Distance_leftright2 = null, htup_Distance_leftright3 = null, htup_Distance_leftright4 = null; htup_Distance_leftright = null;
        HOperatorSet.DistancePl(htup_pt_row_lineup_left, htup_pt_col_lineup_left, htup_pt_row_linedown_left,
            htup_pt_col_linedown_left, htup_pt_row_linedown_right, htup_pt_col_linedown_right,
            out htup_Distance_updown1);
        HOperatorSet.DistancePl(htup_pt_row_lineup_right, htup_pt_col_lineup_right, htup_pt_row_linedown_left,
            htup_pt_col_linedown_left, htup_pt_row_linedown_right, htup_pt_col_linedown_right,
            out htup_Distance_updown2);
        HOperatorSet.DistancePl(htup_pt_row_linedown_left, htup_pt_col_linedown_left, htup_pt_row_lineup_left,
            htup_pt_col_lineup_left, htup_pt_row_lineup_right, htup_pt_col_lineup_right, out htup_Distance_updown3);
        HOperatorSet.DistancePl(htup_pt_row_linedown_right, htup_pt_col_linedown_right, htup_pt_row_lineup_left,
            htup_pt_col_lineup_left, htup_pt_row_lineup_right, htup_pt_col_lineup_right, out htup_Distance_updown4);
        htup_Distance_updown = (((htup_Distance_updown1 + htup_Distance_updown2) + htup_Distance_updown3) + htup_Distance_updown4) / 4;
        HOperatorSet.DistancePl(htup_pt_row_lineleft_up, htup_pt_col_lineleft_up, htup_pt_row_lineright_up,
            htup_pt_col_lineright_up, htup_pt_row_lineright_down, htup_pt_col_lineright_down,
            out htup_Distance_leftright1);
        HOperatorSet.DistancePl(htup_pt_row_lineleft_down, htup_pt_col_lineleft_down, htup_pt_row_lineright_up,
            htup_pt_col_lineright_up, htup_pt_row_lineright_down, htup_pt_col_lineright_down,
            out htup_Distance_leftright2);
        HOperatorSet.DistancePl(htup_pt_row_lineright_up, htup_pt_col_lineright_up, htup_pt_row_lineleft_up,
            htup_pt_col_lineleft_up, htup_pt_row_lineleft_down, htup_pt_col_lineleft_down,
            out htup_Distance_leftright3);
        HOperatorSet.DistancePl(htup_pt_row_lineright_down, htup_pt_col_lineright_down, htup_pt_row_lineleft_up,
            htup_pt_col_lineleft_up, htup_pt_row_lineleft_down, htup_pt_col_lineleft_down,
            out htup_Distance_leftright4);
        htup_Distance_leftright = (((htup_Distance_leftright1 + htup_Distance_leftright2) + htup_Distance_leftright3) + htup_Distance_leftright4) / 4;
        //
        //**************************对角线长度
        HOperatorSet.DistancePp(htup_TopLeftRow, htup_TopLeftCol, htup_BottomRightRow, htup_BottomRightCol,
            out htup_LengthDiagonal1);
        HOperatorSet.DistancePp(htup_BottomLeftRow, htup_BottomLeftCol, htup_TopRightRow, htup_ToprightCol,
            out htup_LengthDiagonal2);
        //**************************对角线夹角
        HTuple htup_Angle_diagonal;
        HOperatorSet.AngleLl(htup_BottomLeftRow, htup_BottomLeftCol, htup_TopRightRow, htup_ToprightCol,
            htup_TopLeftRow, htup_TopLeftCol, htup_BottomRightRow, htup_BottomRightCol, out htup_Angle_diagonal);
        htup_Angle_diagonalDeg = ((htup_Angle_diagonal.TupleAbs())).TupleDeg();
        if ((int)(new HTuple(htup_Angle_diagonalDeg.TupleGreater(90))) != 0)
        {
            htup_Angle_diagonalDeg = 180 - htup_Angle_diagonalDeg;
        }
        //**************************外切圆同心度
        //TODO
        //**************************孔洞
        // htup_hole_count

        //**************************边角角度
        //htup_AngleTopLeft, htup_AngleTopRight, 
        //htup_AngleBottomLeft, htup_AngleBottomRight

        //**************************倒角弧长
        HTuple htup_distance_tmpleftup, htup_distance_tmprightdown, htup_distance_tmpleftdown, htup_distance_tmprightup;
        HOperatorSet.DistancePp(htup_pt_row_lineup_left, htup_pt_col_lineup_left, htup_pt_row_lineleft_up,
            htup_pt_col_lineleft_up, out htup_distance_tmpleftup);
        htup_arc_leftup = (2 * ((((0.5 * htup_distance_tmpleftup) / htup_Radiuslurd)).TupleAsin()
            )) * htup_Radiuslurd;
        HOperatorSet.DistancePp(htup_pt_row_linedown_right, htup_pt_col_linedown_right, htup_pt_row_lineright_down,
            htup_pt_col_lineright_down, out htup_distance_tmprightdown);
        htup_arc_rightdown = (2 * ((((0.5 * htup_distance_tmprightdown) / htup_Radiuslurd)).TupleAsin()
            )) * htup_Radiuslurd;
        HOperatorSet.DistancePp(htup_pt_row_lineleft_down, htup_pt_col_lineleft_down, htup_pt_row_linedown_left,
            htup_pt_col_linedown_left, out htup_distance_tmpleftdown);
        htup_arc_leftdown = (2 * ((((0.5 * htup_distance_tmpleftdown) / htup_Radiusldru)).TupleAsin()
            )) * htup_Radiusldru;
        HOperatorSet.DistancePp(htup_pt_row_lineright_up, htup_pt_col_lineright_up, htup_pt_row_lineup_right,
            htup_pt_col_lineup_right, out htup_distance_tmprightup);
        htup_arc_rightup = (2 * ((((0.5 * htup_distance_tmprightup) / htup_Radiusldru)).TupleAsin()
            )) * htup_Radiusldru;
        //**********************同心度
        HOperatorSet.DistancePp(htup_Rowall, htup_Columnall, htup_centerRow,
          htup_centerColumn, out htup_CenterDiff);


    }
    //*******************************计算多晶硅参数************************
    public void CalPolycrystalParamter()
    {
        //**************************总边长
        //
        htup_Distance_upline = null; htup_Distance_rightline = null; htup_Distance_leftline = null; htup_Distance_downline = null;
        HOperatorSet.DistancePp(htup_TopLeftRow, htup_TopLeftCol, htup_TopRightRow, htup_ToprightCol,
            out htup_Distance_upline);
        HOperatorSet.DistancePp(htup_TopRightRow, htup_ToprightCol, htup_BottomRightRow, htup_BottomRightCol,
            out htup_Distance_rightline);
        HOperatorSet.DistancePp(htup_TopLeftRow, htup_TopLeftCol, htup_BottomLeftRow, htup_BottomLeftCol,
            out htup_Distance_leftline);
        HOperatorSet.DistancePp(htup_BottomLeftRow, htup_BottomLeftCol, htup_BottomRightRow, htup_BottomRightCol,
            out htup_Distance_downline);
        //**************************边长
        //HTuple htup_Distance_topside = null, htup_Distance_leftside = null, htup_Distance_bottomside = null, htup_Distance_rightside = null;
        //HOperatorSet.DistancePp(htup_pt_row_lineup_left, htup_pt_col_lineup_left, htup_pt_row_lineup_right,
        //    htup_pt_col_lineup_right, out htup_Distance_topside);
        //HOperatorSet.DistancePp(htup_pt_row_lineleft_up, htup_pt_col_lineleft_up, htup_pt_row_lineleft_down,
        //    htup_pt_col_lineleft_down, out htup_Distance_leftside);
        //HOperatorSet.DistancePp(htup_pt_row_linedown_left, htup_pt_col_linedown_left, htup_pt_row_linedown_right,
        //    htup_pt_col_linedown_right, out htup_Distance_bottomside);
        //HOperatorSet.DistancePp(htup_pt_row_lineright_up, htup_pt_col_lineright_up, htup_pt_row_lineright_down,
        //    htup_pt_col_lineright_down, out htup_Distance_rightside);
        //**************************两边距
        HTuple htup_Distance_updown1 = null, htup_Distance_updown2 = null, htup_Distance_updown3 = null, htup_Distance_updown4 = null; htup_Distance_updown = null;
        HTuple htup_Distance_leftright1 = null, htup_Distance_leftright2 = null, htup_Distance_leftright3 = null, htup_Distance_leftright4 = null; htup_Distance_leftright = null;
        HOperatorSet.DistancePl(htup_pt_row_lineup_left, htup_pt_col_lineup_left, htup_pt_row_linedown_left,
            htup_pt_col_linedown_left, htup_pt_row_linedown_right, htup_pt_col_linedown_right,
            out htup_Distance_updown1);
        HOperatorSet.DistancePl(htup_pt_row_lineup_right, htup_pt_col_lineup_right, htup_pt_row_linedown_left,
            htup_pt_col_linedown_left, htup_pt_row_linedown_right, htup_pt_col_linedown_right,
            out htup_Distance_updown2);
        HOperatorSet.DistancePl(htup_pt_row_linedown_left, htup_pt_col_linedown_left, htup_pt_row_lineup_left,
            htup_pt_col_lineup_left, htup_pt_row_lineup_right, htup_pt_col_lineup_right, out htup_Distance_updown3);
        HOperatorSet.DistancePl(htup_pt_row_linedown_right, htup_pt_col_linedown_right, htup_pt_row_lineup_left,
            htup_pt_col_lineup_left, htup_pt_row_lineup_right, htup_pt_col_lineup_right, out htup_Distance_updown4);
        htup_Distance_updown = (((htup_Distance_updown1 + htup_Distance_updown2) + htup_Distance_updown3) + htup_Distance_updown4) / 4;
        HOperatorSet.DistancePl(htup_pt_row_lineleft_up, htup_pt_col_lineleft_up, htup_pt_row_lineright_up,
            htup_pt_col_lineright_up, htup_pt_row_lineright_down, htup_pt_col_lineright_down,
            out htup_Distance_leftright1);
        HOperatorSet.DistancePl(htup_pt_row_lineleft_down, htup_pt_col_lineleft_down, htup_pt_row_lineright_up,
            htup_pt_col_lineright_up, htup_pt_row_lineright_down, htup_pt_col_lineright_down,
            out htup_Distance_leftright2);
        HOperatorSet.DistancePl(htup_pt_row_lineright_up, htup_pt_col_lineright_up, htup_pt_row_lineleft_up,
            htup_pt_col_lineleft_up, htup_pt_row_lineleft_down, htup_pt_col_lineleft_down,
            out htup_Distance_leftright3);
        HOperatorSet.DistancePl(htup_pt_row_lineright_down, htup_pt_col_lineright_down, htup_pt_row_lineleft_up,
            htup_pt_col_lineleft_up, htup_pt_row_lineleft_down, htup_pt_col_lineleft_down,
            out htup_Distance_leftright4);
        htup_Distance_leftright = (((htup_Distance_leftright1 + htup_Distance_leftright2) + htup_Distance_leftright3) + htup_Distance_leftright4) / 4;
        //
        //**************************对角线长度
        HOperatorSet.DistancePp(htup_TopLeftRow, htup_TopLeftCol, htup_BottomRightRow, htup_BottomRightCol,
            out htup_LengthDiagonal1);
        HOperatorSet.DistancePp(htup_BottomLeftRow, htup_BottomLeftCol, htup_TopRightRow, htup_ToprightCol,
            out htup_LengthDiagonal2);
        //**************************对角线夹角
        HTuple htup_Angle_diagonal;
        HOperatorSet.AngleLl(htup_BottomLeftRow, htup_BottomLeftCol, htup_TopRightRow, htup_ToprightCol,
            htup_TopLeftRow, htup_TopLeftCol, htup_BottomRightRow, htup_BottomRightCol, out htup_Angle_diagonal);
        htup_Angle_diagonalDeg = ((htup_Angle_diagonal.TupleAbs())).TupleDeg();
        if ((int)(new HTuple(htup_Angle_diagonalDeg.TupleGreater(90))) != 0)
        {
            htup_Angle_diagonalDeg = 180 - htup_Angle_diagonalDeg;
        }
        //**************************外切圆同心度
        //TODO
        //**************************孔洞
        // htup_hole_count

        //**************************边角角度
        //htup_AngleTopLeft, htup_AngleTopRight, 
        //htup_AngleBottomLeft, htup_AngleBottomRight

        //**************************倒角弧长
        HOperatorSet.DistancePp(htup_pt_row_lineup_left, htup_pt_col_lineup_left, htup_pt_row_lineleft_up,
            htup_pt_col_lineleft_up, out htup_arc_leftup);
        HOperatorSet.DistancePp(htup_pt_row_linedown_right, htup_pt_col_linedown_right, htup_pt_row_lineright_down,
            htup_pt_col_lineright_down, out htup_arc_rightdown);
        HOperatorSet.DistancePp(htup_pt_row_lineleft_down, htup_pt_col_lineleft_down, htup_pt_row_linedown_left,
            htup_pt_col_linedown_left, out htup_arc_leftdown);
        HOperatorSet.DistancePp(htup_pt_row_lineright_up, htup_pt_col_lineright_up, htup_pt_row_lineup_right,
            htup_pt_col_lineup_right, out htup_arc_rightup);

    }
    //*************************为外部程序提供参数接口***********************
    //***************************返回两边距（上下边、左右边）
    public string ReturnEdge1Distance()
    {
        int demicalNum = 3;//保留小数点几位
        HTuple htup_Realvalue = htup_Distance_updown * lengthEachpixel;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
       double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
     }
    public string ReturnEdge2Distance()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_Distance_leftright * lengthEachpixel;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    public string ReturnEdgeLengthU()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_Distance_upline * lengthEachpixel;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    public string ReturnEdgeLengthD()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_Distance_downline * lengthEachpixel;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    public string ReturnEdgeLengthL()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_Distance_leftline * lengthEachpixel;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    public string ReturnEdgeLengthR()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_Distance_rightline * lengthEachpixel;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    //***************************返回弧长（从左上角开始顺时针算）
    public string ReturnArcLength1()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_arc_leftup * lengthEachpixel;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    public string ReturnArcLength2()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_arc_rightup * lengthEachpixel;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    public string ReturnArcLength3()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_arc_leftdown * lengthEachpixel;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    public string ReturnArcLength4()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_arc_rightdown * lengthEachpixel;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    //***************************返回边角角度
    public string ReturnEdgeAngle1()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_AngleTopLeft ;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    public string ReturnEdgeAngle2()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_AngleTopRight;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    public string ReturnEdgeAngle3()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_AngleBottomLeft;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    public string ReturnEdgeAngle4()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_AngleBottomRight;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    //***************************返回对角线长度和夹角
    public string ReturnDiagonalLength1()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_LengthDiagonal1 * lengthEachpixel; ;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    public string ReturnDiagonalLength2()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_LengthDiagonal2 * lengthEachpixel; ;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    public string ReturnDiagonalAngle()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_Angle_diagonalDeg; ;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    //***************************返回孔洞个数
    public string ReturnHoleCount()
    {
        HTuple Ret = htup_hole_count;
        return Ret.ToString();
    }
    //***************************返回拟合圆弧的半径
    public string ReturnArcRadius1() {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_Radiuslu * lengthEachpixel; ;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    public string ReturnArcRadius2()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_Radiusru * lengthEachpixel; ;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    public string ReturnArcRadius3()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_Radiusld * lengthEachpixel; ;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    public string ReturnArcRadius4()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_Radiusrd * lengthEachpixel; ;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    //*************************返回同心度
    public string ReturnCenterDiff()
    {
        int demicalNum = 3;
        HTuple htup_Realvalue = htup_CenterDiff * lengthEachpixel; ;
        string str = htup_Realvalue.ToString();
        int count = str.LastIndexOf(".");
        double Ret = Double.Parse(str.Substring(0, count + demicalNum));
        return Ret.ToString();
    }
    public string ReturnEdgeDetectResult()
    {
        if (bl_IsAreaDetectOK) return "OK";
        else return "NG";
    }
    public void SetStardParameter(double[] dbl_InputParamter)
    {
        dbl_StandEdgeDistance = dbl_InputParamter[0];
        dbl_StandEdgeLength = dbl_InputParamter[1];
        dbl_StandArcLength = dbl_InputParamter[2];
        dbl_StandArcAngle = dbl_InputParamter[3];
        dbl_StandDiagonalLength = dbl_InputParamter[4];
        dbl_StandDiagonalAngle = dbl_InputParamter[5];
        dbl_StandArcRadius= dbl_InputParamter[6];
        dbl_MaxCenterDiff = dbl_InputParamter[7];
        dbl_LengthPrecision = dbl_InputParamter[8];
        dbl_AnglePrecision = dbl_InputParamter[9];
    }


    public void IsAreaDetectOK()
    {
        if (htup_hole_count > 0) bl_IsAreaDetectOK = false;
        else
        {
            double[] LengthDiff = new double[17];
            double dbl_Distance_leftright = (htup_Distance_leftright* lengthEachpixel).TupleSelect(0);
            double dbl_Distance_updown = (htup_Distance_updown * lengthEachpixel).TupleSelect(0);
            double dbl_Distance_topside = (htup_Distance_upline * lengthEachpixel).TupleSelect(0);
            double dbl_Distance_rightside = (htup_Distance_rightline * lengthEachpixel).TupleSelect(0);
            double dbl_Distance_bottomside = (htup_Distance_downline * lengthEachpixel).TupleSelect(0);
            double dbl_Distance_leftside = (htup_Distance_leftline * lengthEachpixel).TupleSelect(0);
            double dbl_LengthDiagonal1 = (htup_LengthDiagonal1 * lengthEachpixel).TupleSelect(0);
            double dbl_LengthDiagonal2 = (htup_LengthDiagonal2 * lengthEachpixel).TupleSelect(0);
            double dbl_arc_leftup = (htup_arc_leftup * lengthEachpixel).TupleSelect(0);
            double dbl_arc_rightup = (htup_arc_rightup * lengthEachpixel).TupleSelect(0);
            double dbl_arc_rightdown = (htup_arc_rightdown * lengthEachpixel).TupleSelect(0);
            double dbl_arc_leftdown = (htup_arc_leftdown * lengthEachpixel).TupleSelect(0);
            //double dbl_Radiuslu = (htup_Radiuslu * lengthEachpixel).TupleSelect(0);
            //double dbl_Radiusld = (htup_Radiusld * lengthEachpixel).TupleSelect(0);
            //double dbl_Radiusru = (htup_Radiusru * lengthEachpixel).TupleSelect(0);
            //double dbl_Radiusrd = (htup_Radiusrd * lengthEachpixel).TupleSelect(0);
            LengthDiff[0] = System.Math.Abs(dbl_Distance_leftright - dbl_StandEdgeDistance);
            LengthDiff[1] = System.Math.Abs(dbl_Distance_updown - dbl_StandEdgeDistance);
            LengthDiff[2] = System.Math.Abs(dbl_Distance_topside - dbl_StandEdgeLength);
            LengthDiff[3] = System.Math.Abs(dbl_Distance_rightside - dbl_StandEdgeLength);
            LengthDiff[4] = System.Math.Abs(dbl_Distance_bottomside - dbl_StandEdgeLength);
            LengthDiff[5] = System.Math.Abs(dbl_Distance_leftside - dbl_StandEdgeLength);
            LengthDiff[6] = System.Math.Abs(dbl_LengthDiagonal1 - dbl_StandDiagonalLength);
            LengthDiff[7] = System.Math.Abs(dbl_LengthDiagonal2 - dbl_StandDiagonalLength);
            LengthDiff[8] = System.Math.Abs(dbl_arc_leftup - dbl_StandArcLength);
            LengthDiff[9] = System.Math.Abs(dbl_arc_rightup - dbl_StandArcLength);
            LengthDiff[10] = System.Math.Abs(dbl_arc_rightdown - dbl_StandArcLength);
            LengthDiff[11] = System.Math.Abs(dbl_arc_leftdown - dbl_StandArcLength);
            LengthDiff[12] = System.Math.Abs(dbl_arc_leftdown - dbl_StandArcLength);
            //LengthDiff[13] = System.Math.Abs(dbl_Radiuslu - dbl_StandArcRadius);
            //LengthDiff[14] = System.Math.Abs(dbl_Radiusld - dbl_StandArcRadius);
            //LengthDiff[15] = System.Math.Abs(dbl_Radiusru - dbl_StandArcRadius);
            //LengthDiff[16] = System.Math.Abs(dbl_Radiusrd - dbl_StandArcRadius);
            double MinLengthDiff = LengthDiff.Max();
            double[] AngleDiff=new double[5];
            double dbl_Angle_diagonalDeg = htup_Angle_diagonalDeg.TupleSelect(0);
            double dbl_AngleTopLeft = htup_Angle_diagonalDeg.TupleSelect(0);
            double dbl_AngleTopRight = htup_Angle_diagonalDeg.TupleSelect(0);
            double dbl_AngleBottomLeft = htup_Angle_diagonalDeg.TupleSelect(0);
            double dbl_AngleBottomRight = htup_Angle_diagonalDeg.TupleSelect(0);
            AngleDiff[0]= System.Math.Abs(dbl_Angle_diagonalDeg - dbl_StandDiagonalAngle);
            AngleDiff[1] = System.Math.Abs(dbl_AngleTopLeft - dbl_StandDiagonalAngle);
            AngleDiff[2] = System.Math.Abs(dbl_AngleTopRight - dbl_StandDiagonalAngle);
            AngleDiff[3] = System.Math.Abs(dbl_AngleBottomLeft - dbl_StandDiagonalAngle);
            AngleDiff[4] = System.Math.Abs(dbl_AngleBottomRight - dbl_StandDiagonalAngle);
            double MinAngleDiff = AngleDiff.Max();
            if (MinAngleDiff < dbl_AnglePrecision && MinLengthDiff < dbl_LengthPrecision)
            { bl_IsAreaDetectOK = true; }
            else bl_IsAreaDetectOK = false;
        }
        

    }





    public void Is_empty()
    {

        if ((this.htup_Meanbackgroud - this.htup_TestMeanGrey) < 50)
        {
            MessageBox.Show("empty");
        }
    }
    public void IsOK_line()
    {
        HTuple LarryLength;
        LarryLength = 0;
        HOperatorSet.TupleLength(htup_TopsideRowArray, out LarryLength);
        if ((LarryLength) < 50)
        {
            MessageBox.Show("upLine wrong");
        }
        LarryLength = 0;
        HOperatorSet.TupleLength(htup_LeftsideRowArray, out LarryLength);
        if ((LarryLength) < 50)
        {
            MessageBox.Show("lefLine wrong");
        }
        LarryLength = 0;
        HOperatorSet.TupleLength(htup_BottomRowArray, out LarryLength);
        if ((LarryLength) < 50)
        {
            MessageBox.Show("bottomLine wrong");
        }
        LarryLength = 0;
        HOperatorSet.TupleLength(htup_RightsideRowArray, out LarryLength);
        if ((LarryLength) < 50)
        {
            MessageBox.Show("rightLine wrong");
        }
    }
    public void IsOK_cricle()
    {
        HTuple LarryLength;
        LarryLength = 0;
        HOperatorSet.TupleLength(htup_TempRowEdgelu, out LarryLength);
        if ((LarryLength) < 50)
        {
            MessageBox.Show("TopLeftside circle wrong");
        }
        LarryLength = 0;
        HOperatorSet.TupleLength(htup_TempRowEdgeru, out LarryLength);
        if ((LarryLength) < 50)
        {
            MessageBox.Show("TopRightside circle wrong");
        }
        LarryLength = 0;
        HOperatorSet.TupleLength(htup_TempRowEdgeld, out LarryLength);
        if ((LarryLength) < 50)
        {
            MessageBox.Show("BottomLeftside circle wrong");
        }
        LarryLength = 0;
        HOperatorSet.TupleLength(htup_TempRowEdgerd, out LarryLength);
        if ((LarryLength) < 50)
        {
            MessageBox.Show("BottomRightside circle wrong");
        }
    }
    public void ClearAll() {
        
    }

    public void SendResult()
    {
        //发送结果给上位机和PLC
    }
}
